import axios from "./axios";

export interface AddressList {
    tel: string
    nickname: string
    provice_city_area: string
    detail_address: string
    longitude: string
    latitude: string
}
export interface CodeList {
    createTime: string
    goods: Array<object>
    address: object
    totalPrice: number
}
export interface OrderList {
    state: string
}
export interface OrderPay {
    out_trade_no: string
}
export interface OrderDelete {
    id: string
}

// 地址数据接口（包含id字段）
export interface AddressData extends AddressList {
    id: string  // 地址ID，字符串类型
    delivery?: boolean  // 是否默认地址
}

export interface PayList {
    id: string
    returnUrl: string
}

export const getaddAddress = async (data: AddressList) => {
    console.log(data);
    const res = await axios.put('/tx/v1/address', data)
    console.log(res.data);
    return res.data || []
}
//添加收货地址
export const getaddAddressList = async (): Promise<{ data: AddressData[] }> => {
    const res = await axios.get('/tx/v1/address')
    console.log(res.data);
    // 确保返回的数据中id是字符串类型
    const data = res.data?.data || []
    const normalizedData = data.map((item: any) => ({
        ...item,
        id: String(item.id)  // 确保id是字符串类型
    }))
    return { data: normalizedData }
}
//修改收货地址
export const getEditAddress = async (data: AddressList) => {
    const res = await axios.post('/tx/v1/address', data)
    console.log(res.data);
    return res.data || []
}

//获取单项收货地址
export const getSingleAddress = async (id: string): Promise<{ data: AddressData }> => {
    const res = await axios.get(`/tx/v1/address/${id}`)
    console.log(res.data);
    // 确保返回的数据中id是字符串类型
    const data = res.data?.data || {}
    const normalizedData = {
        ...data,
        id: String(data.id)  // 确保id是字符串类型
    }
    return { data: normalizedData }
}

//删除收货地址
export const getDeleteAddress = async (id: string) => {
    const res = await axios.delete(`/tx/v1/address/${id}`)
    console.log(res.data);
    return res.data || []
}

// 地址选择工具函数 - 智能选择默认地址
export const getDefaultAddress = (addressList: AddressData[]): AddressData | null => {
    if (!addressList || addressList.length === 0) {
        return null
    }
    if (addressList.length === 1) {
        return addressList[0] // 只有一个地址。自动设为默认
    }
    const defaultAddress = addressList.find(addr => addr.delivery === true)
    if (defaultAddress) {
        return defaultAddress
    }
    return addressList[0]
}

// 支付接口
export const geCode = async (data: CodeList) => {
    const res = await axios.post('/tx/v1/place/order', data)
    return res.data || []
}

export const getPay = async (data: PayList) => {
    const res = await axios.post('/tx/v1/payment', data)
    return res.data || []
}

//获取订单列表
export const getOrderList = async (data: OrderList) => {
    const res = await axios.get('/tx/v1/order', { params: data });
    console.log(res.data);
    return res.data || [];
}

//订单状态数量查询
export const getOrderState = async () => {
    const res = await axios.get('/tx/v1/order/count');
    console.log(res.data);
    return res.data || [];
}

//支付状态查询
export const getOrderPay = async (data: OrderPay) => {
    const res = await axios.get('/tx/v1/order/query', { params: data });
    console.log(res.data);
    return res.data || [];
}

//订单删除
export const getOrderDelete = async (data: OrderDelete) => {
    const res = await axios.delete('/tx/v1/order', { params: data });
    console.log(res.data);
    return res.data || [];
}
